package com.sunchangpeng.wechat.mp.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import com.sunchangpeng.wechat.mp.user.bean.User;
import com.sunchangpeng.wechat.mp.user.bean.UserPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class Users {
    private WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static Users with(MpAppSetting appSetting) {
        Users users = new Users();
        users.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return users;
    }

    /**
     * 获取用户基本信息
     */
    public User info(String openId) {
        return info(openId, "zh_CN");
    }

    /**
     * 获取用户基本信息
     */
    public User info(String openId, String lang) {
        String url = EndpointProvider.get("url.user.get");
        String user = wechatClient.getTextWithToken(String.format(url, openId, lang));
        return JSON.parseObject(user, User.class);
    }

    /**
     * 批量获取用户基本信息
     */
    public List<User> batchInfo(String... openIds) {
        return batchInfo(Arrays.stream(openIds)
                .map(openid -> new JSONObject().fluentPut("openid", openid).fluentPut("lang", "zh_CN"))
                .collect(Collectors.toList()));
    }

    /**
     * 批量获取用户基本信息
     */
    public List<User> batchInfo(List<JSONObject> openIds) {
        String url = EndpointProvider.get("url.user.batchget");
        String users = wechatClient.getTextWithToken(String.format(url), JSON.toJSONString(new JSONObject().fluentPut("user_list", openIds)));
        return JSON.parseObject(users, UsersWrapper.class).getList();
    }

    /**
     * 获取用户列表
     */
    public UserPage get() {
        return get(null);
    }

    /**
     * 获取用户列表
     */
    public UserPage get(String nextOpenId) {
        String url = EndpointProvider.get("url.user.list");
        if (StringUtils.isNotBlank(nextOpenId)) {
            url = url + "?next_openid=" + nextOpenId;
        }
        String response = wechatClient.getTextWithToken(url);
        return JSON.parseObject(response, UserPage.class);
    }

    /**
     * 设置用户备注名
     */
    public void remark(String openId, String remark) {
        String url = EndpointProvider.get("url.user.remark");
        JSONObject data = new JSONObject().fluentPut("openid", openId).fluentPut("remark", remark);
        wechatClient.getTextWithToken(url, JSON.toJSONString(data));
    }

    static class UsersWrapper {
        @JSONField(name = "user_info_list")
        private List<User> list;

        public List<User> getList() {
            return list;
        }

        public void setList(List<User> list) {
            this.list = list;
        }
    }
}
